package com.example.weather.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Administrator on 2017/5/27 0027.
 */

public class HttpUtil {
    public static  void sendHttprequest(final String address, final HttpCallbackListener listener)
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                try {
                    URL url = new URL(address);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);
                    InputStream in = connection.getInputStream();
                    BufferedReader reader =new BufferedReader(new InputStreamReader(in));
                    StringBuilder response =  new StringBuilder();
                    String line ;
                    while ((line = reader.readLine()) != null){
                        response.append(line);
                    }
                    if (listener != null){
                        //回掉onFinish（）方法
                        listener.onFinish(response.toString());
                    }
                } catch (MalformedURLException e) {
                    // 回调onError()方法
                    if (listener != null) {
                        listener.onError(e);
                    }
                } catch (IOException e) {
                    // 回调onError()方法
                    if (listener != null) {
                        listener.onError(e);
                    }
                }
            }
        }).start();
    }
}
